package com.qian.service.ddj.dao;

import com.qian.service.ddj.entity.DdjOrder;
import com.qian.service.ddj.entity.DdjUser;
import com.yaowk.service.common.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Repository("DdjUserRepository")
public interface UserRepository extends BaseRepository<DdjUser, Integer> {

    @Modifying
    @Query(value = "update ddj_user set balance = balance + ?2 where id = ?1", nativeQuery = true)
    int increaseBalance(Integer userId, BigDecimal balance);

    @Modifying
    @Query(value = "update ddj_user set balance = balance - ?2 where id = ?1", nativeQuery = true)
    int decreaseBalance(Integer userId, BigDecimal balance);

    DdjUser findByUsername(String username);

    @Query(value = "select * from ddj_order where user_id=?1",nativeQuery = true)
    List<DdjOrder> QueryOrderByUserId(Integer userId);

    @Modifying
    @Query(value = "update ddj_user set balance = balance - ?2, lock_money = lock_money + ?2 where id = ?1", nativeQuery = true)
    void lockMoney(Integer userId, BigDecimal money);

    @Modifying
    @Query(value = "update ddj_user set balance = balance + ?2, lock_money = lock_money - ?2 where id = ?1", nativeQuery = true)
    void unLockMoney(Integer userId, BigDecimal money);
}
